package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.banner.CreateOrUpdateBannerDTO;
import io.github.talelin.latticy.model.BannerDO;
import io.github.talelin.latticy.service.BannerService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
* @author generator@lms
* @since 2021-04-18
*/
@RestController
@RequestMapping("/v1/banner")
@Validated
@PermissionModule("Banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @PostMapping("")
    @PermissionMeta("创建Banner")
    @GroupRequired
    @Logger(template = "{user.nickname}创建了一个Banner")
    public CreatedVO create(@RequestBody @Validated CreateOrUpdateBannerDTO dto) {
        bannerService.create(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @PermissionMeta("更新Banner")
    @GroupRequired
    @Logger(template = "{user.nickname}更新了一个Banner")
    public UpdatedVO update(
            @RequestBody @Validated CreateOrUpdateBannerDTO dto,
            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        bannerService.update(dto, id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta("删除Banner")
    @GroupRequired
    @Logger(template = "{user.nickname}删除了一个Banner")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        BannerDO banner = bannerService.getById(id);
        if (banner == null) {
            throw new NotFoundException(30000);
        }
        bannerService.getBaseMapper().deleteById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @LoginRequired
    public BannerDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        BannerDO banner = bannerService.getById(id);
        if (banner == null) {
            throw new NotFoundException(30000);
        }
        return banner;
    }

    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO<BannerDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<BannerDO> pager = new Page<>(page, count);
        IPage<BannerDO> paging = bannerService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

}
